#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>

#define __DEBUG
#undef __DEBUG

int const maxn = 200007;
int const maxm = 300007;
bool vis[maxn];
bool vis_edge[maxm];
int indegree[maxn];
int n, m;

struct edge { int index; int to; int from; };

typedef std::vector<edge> edge_type;
typedef edge_type::iterator edge_it;
std::vector<edge_type> graph;
std::vector<int> edge_color;
std::vector<edge> edge_in;

int dfs(int x)
{
	vis[x] = true;
	int sum = (indegree[x]&1);
	for (edge_it it = graph[x].begin(); it != graph[x].end(); ++it) {
		int v = (*it).to;
		if (vis[v]) continue;
		sum += dfs(v);
		if (sum > 2) return sum;
	}
	return sum;
}

bool eulerian_tour_odd_path(int x, int co) // 1: white  0: black
{
	for (edge_it it = graph[x].begin(); it != graph[x].end(); ++it) {
		int v = (*it).to, index = (*it).index;
		if (vis_edge[index]) continue;
		vis_edge[index] = true;
		if (x == edge_in[index].from) edge_color[index] = 1-co;
		else						  edge_color[index] = co;
		indegree[x]--; indegree[v]--;
		if (eulerian_tour_odd_path(v, 1 - co)) return true;
	}
	return true;
}

std::deque<edge> euler_tour;
void eulerian_tour(int x, int id)
{
	for (edge_it it = graph[x].begin(); it != graph[x].end(); ++it) {
		int v = (*it).to, index = (*it).index;
		if (vis_edge[index]) continue;
		vis_edge[index] = true;
		indegree[x]--; indegree[v]--;
		eulerian_tour(v, index);
	}
	edge tmp;
	tmp.to = x; tmp.index = id;
	euler_tour.push_back(tmp);
}

void color_euler_tour()
{
	for (int i = 1, co = 1; i < (int)euler_tour.size(); i++) {
		int index = euler_tour[i].index;
		int u = euler_tour[i - 1].to;
		if (u == edge_in[index].from) edge_color[index] = co;
		else						  edge_color[index] = 1 - co;
		co = 1 - co;
	}
}

int main()
{
	std::ios::sync_with_stdio(false);
	int T; std::cin >> T;
	while (T--) {
		std::cin >> n >> m;

#ifdef __DEBUG
		std::cerr << "----------------------------------hello world\n";
#endif

		graph.clear();
		graph.resize(n);
		edge_color.resize(m);
		edge_in.resize(m);
		for (int i = 0; i < n; i++) indegree[i] = 0;

		for (int i = 0, x, y; i < m; i++) {
			std::cin >> x >> y;
			x--; y--;
			edge tmp; tmp.index = i;
			tmp.to = y; graph[x].push_back(tmp);
			tmp.to = x; graph[y].push_back(tmp);
			tmp.from = x; tmp.to = y;
			edge_in[i] = tmp;
			indegree[x]++; indegree[y]++;
		}

		for (int i = 0; i < n; i++)
			if (indegree[i] & 1) {
				int odd_degree = 4;
				if (odd_degree == 2) {
					euler_tour.clear();
					eulerian_tour(i, 0); // FIXME
					color_euler_tour();
				} else {
					eulerian_tour_odd_path(i, 1);
				}
			}

		for (int i = 0; i < n; i++)
			if (indegree[i]) {
				euler_tour.clear();
				eulerian_tour(i, 0); // FIXME
				/*
				for (int count = euler_tour.size(); count
						&& graph[euler_tour[0]].size() <= 2; count--) {
					euler_tour.pop_front();
					euler_tour.push_back(euler_tour[0]);
				}
				*/
				color_euler_tour();
			}


		for (int i = 0; i < m; i++)
			std::cout << edge_color[i] << '\n';
	}
}

